<?php

namespace app\controller\admin\wz;

use http\Client;
use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Equipment
 * @package app\controller\admin\run
 */
class Equipment extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'asc'];

    public function _initialize()
    {

    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        try{
            $postData   = $this->buildPostParams();
            $data = $this->postData("getEquipmentList",$postData);
            $responseData = $data['data'];
            $responseData['data'] = $responseData['list'];
            unset($responseData['list']);
            return $this->success('数据获取成功', $responseData);
        }catch (\Exception $e){
            return $this->error($e->getMessage(),-1);
        }

    }


    //查看详情
    public function info()
    {
        try{
            $id   = $this->request->param('id');
            $response =  $this->postData("readEquipment",['id'=> $id]);
            $info = $response['data'];
            if(isset($info['icon'])){
                $info['icon'] = imgUrlToAbsolute($info['icon']);
            }
            return $this->success('获取成功', $info);
        }catch (\Exception $e){
            return $this->error($e->getMessage(),-1);
        }

    }


    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());
        try{
            //$icon = parse_url($post['icon']);
            $response = $this->postData("createEquipment",[
                'name' => $post['name'],
                'icon' => $post['icon']
            ]);
            if ($response['code'] == 0) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error($response['message']);
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //编辑
    public function edit()
    {
        $id   = $this->request->param('id');
        $response =  $this->postData("readEquipment",['id'=> $id]);
        if (empty($response['data'])) {
            return $this->error('ID参数错误');
        }
        $post = CommonFun::filterPostData($this->request->post());
        try {
            //$icon = parse_url($post['icon']);
            $response = $this->postData("updateEquipment",[
                'id' => $id,
                'name' => $post['name'],
                'icon' => $post['icon']
            ]);
            if ($response['code'] == 0) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error($response['message']);
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    //删除
    public function del()
    {
        $ids = array_filter($this->request->post('ids'));
        if (!$ids) {
            return $this->error('参数ids不能为空');
        }
        try{
            $response = $this->postData("deleteEquipment",[
                'id' => $ids[0],
            ]);
            if ($response['code'] == 0) {
                $this->clearCache();
                return $this->success('删除成功');
            } else {
                return $this->error($response['message']);
            }
        }catch (\Exception $e){
            return $this->exceptionError($e);
        }
    }

}